package com.zch.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.base.dialect.PaginationSupport;
import com.base.support.ExcelColumns;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;

import com.base.util.DateUtil;
import com.base.util.ExcelExporter;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import com.item.service.MessageService;

import org.springframework.beans.factory.annotation.Autowired;
import com.base.action.CoreController;

import com.zch.dao.model.CompanyRegister;
import com.zch.dao.model.CompanyRegisterExample;
import com.zch.dao.model.ManagerFollow;
import com.zch.dao.model.ProjectConnect;
import com.zch.dao.model.ProjectMessage;
import com.zch.dao.model.Zaiti;
import com.zch.daoEx.model.CompanyRegisterEx;
import com.zch.daoEx.model.ZaitiEx;
import com.zch.service.CompanyRegisterService;
import com.zch.service.ProjectMessageService;

@RequestMapping("companyRegister")
@Controller
@LoginFilter
public class CompanyRegisterController extends CoreController{

	@Autowired
	private CompanyRegisterService companyRegisterService;
	@Autowired
    private ProjectMessageService projectMessageService;
	@Autowired
    private MessageService messageService;
	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows, String name){
		PaginationSupport.byPage(page, rows);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		List<CompanyRegisterEx> list = companyRegisterService.selectList(map);
		return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(CompanyRegister companyRegister){
		if (StringUtils.isBlank(companyRegister.getId())){
			companyRegisterService.insert(companyRegister);
		}else{
			companyRegisterService.updateByPrimaryKeySelective(companyRegister);
			CompanyRegister companyRegister2 = companyRegisterService.selectByPrimaryKey(companyRegister.getId());
            
            ProjectMessage ex = new ProjectMessage();
            ex.setId(companyRegister2.getProjectId());
            ex.setIsEnable(1);
            projectMessageService.updateByPrimaryKeySelective(ex);
            
            companyRegister = companyRegisterService.selectByPrimaryKey(companyRegister.getId());
            String projectId  =companyRegister.getProjectId();
            
            ProjectMessage projectMessage = projectMessageService.selectByPrimaryKey(projectId);
            String pushContent = "";
            if (companyRegister.getIsAgree() == 2){
                pushContent = "审核未通过!原因"+companyRegister.getNotAgreeReason();
                projectMessage.setNotAgreeReason(companyRegister.getNotAgreeReason());
                projectMessage.setIsEnable(1);
                projectMessage.setIsAgree(2);
                projectMessageService.updateByPrimaryKeySelective(projectMessage);
                //推送给项目发布人
                messageService.findMessage(projectMessage.getTitle(), pushContent, projectMessage.getUserId(), 1);
                //推送给项目可行性分析人
                messageService.findMessage(projectMessage.getTitle(), pushContent, projectMessage.getAnalyzeMaster(), 1);
                //推送给项目对接人
                messageService.findMessage(projectMessage.getTitle(), pushContent, projectMessage.getConnectMaster(), 1);
            }
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String id){
		CompanyRegister companyRegister = companyRegisterService.selectByPrimaryKey(id);
		companyRegister.setRanges(companyRegister.getRanges().replaceAll("\n", "<br/>").replaceAll("\\s", "&nbsp;"));
		return JSONUtils.serialize(companyRegister);
	}

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			companyRegisterService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}

    @RequestMapping("/update")
    @ResponseBody
    public String update(CompanyRegister companyRegister){
        this.companyRegisterService.updateByPrimaryKeySelective(companyRegister);
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/saveRegisterMaster")
    @ResponseBody
    public String saveConnectMaster(String  userId,String id){
        if (StringUtils.isNotBlank(userId)){
            int result = companyRegisterService.updateRegisterMaster(userId,id);
            if(result == 1){
                CompanyRegister companyRegister = companyRegisterService.selectByPrimaryKey(id);
                ProjectMessage projectMessage = projectMessageService.selectByPrimaryKey(companyRegister.getProjectId());
                //推送给发布人
                messageService.findMessage(projectMessage.getTitle(), "审核通过！", companyRegister.getUserId(), 2);
                
                //推送给项目公司注册人员
                String pushContent2 = "您有一个项目需要进行跟踪汇报！";
                messageService.findMessage(projectMessage.getTitle(), pushContent2, userId, 2);
            }
        }
        return JSONUtils.serialize(new Ret(0));
    } 
    
    @RequestMapping("/myList")
    @ResponseBody
    public String myList(Integer page, Integer rows, String name, String finalZaiti){
        PaginationSupport.byPage(page, rows);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", name);
        map.put("finalZaiti", finalZaiti);
        List<CompanyRegisterEx> list = companyRegisterService.selectMyList(map);
        return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
    }
    
    @RequestMapping("/exportExcel")
    @ResponseBody
    public void exportExcel(HttpServletResponse response, String id) throws Exception{
        Map<String, Object> map = new HashMap<String ,Object>();
        
        List<CompanyRegisterEx> list = companyRegisterService.selectMyList(map);
        ExcelExporter exporter = new ExcelExporter();
        
        ExcelColumns[] headers = new ExcelColumns[]{
                new ExcelColumns("zaitiName","载体名称"),
                new ExcelColumns("registerName","工商登记名称"),
                new ExcelColumns("companyName","企业名称"),
                new ExcelColumns("address","地址"),
                new ExcelColumns("registerMoney","注册资金(万元)"),
                new ExcelColumns("registerDate","注册日期"),
                new ExcelColumns("master","法人代表"),
                new ExcelColumns("phone","联系电话"),
                new ExcelColumns("ranges","经营范围")
        };
        exporter.createWork(list, headers);
        exporter.export(response, null);
    }
}